<?php

const  WECHATSDK_PATH = __DIR__. DIRECTORY_SEPARATOR ;
const  WECHATSDK_COMP_PATH = WECHATSDK_PATH. 'Components'.DIRECTORY_SEPARATOR;
const  WECHATSDK_LOG_PATH = WECHATSDK_PATH.'Logs'.DIRECTORY_SEPARATOR;
const  WECHATSDK_MATERIAL_PATH= WECHATSDK_PATH.'material'.DIRECTORY_SEPARATOR;

require_once(WECHATSDK_COMP_PATH.'Validate.php');

/**
 * 发送接收者调度、由接收，发送消息共用
 * @author guibinyu
 * @date 2017-06-15 04:14:50
 */
class Wechat
{
    static private $cache = array();
    
    public $appid;
    public $secret;
    public $token;
    static public $access_token='';
    private $components = array('Validate','Menu','Media','ReplyMessage','Receive','Template','CustomAccount','CustomSession','CustomMessage','MassMessage','Oauth','Script','User');
    public function __construct($appid,$secret,$token)
    {
         $this->appid = $appid;
        $this->secret = $secret;
        $this->token = $token;
    }
    public function __get($type)
    {
        
        $type = ucfirst($type);
        if(in_array($type, $this->components))
        {
            require_once(WECHATSDK_COMP_PATH.$type.'.php');
        }
        else
        {
            throw new Exception(1,"components [{$type}] not exists");
        }
       
       return $this->returnHandle($type);
    }
    public function returnHandle($type)
    {
        $index = md5(strtolower($type));
        if (!isset(self::$cache[$index])) {
            $comp = "\\WechatSDK\Components\\$type";
            self::$cache[$index] = new $comp($this->appid,$this->secret,$this->token);
        }
        self::$cache[$index]->access_token = self::$access_token;
        return self::$cache[$index];
    }
    public function setAccessToken($access_token='')
    {
        $Validate = $this->returnHandle('Validate');
        $access_token = $Validate->setAccessToken($access_token);
        self::$access_token = $access_token;
    }
    public function getAccessToken()
    {
        $Validate = $this->returnHandle('Validate');
        return $Validate->getAccessToken();
    }
}

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

